package com.chinaunicom.system.service.impl;

import java.util. List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.chinaunicom.system.dao.UserMapper;
import com.chinaunicom.system.dto.UserDto;
import com.chinaunicom.system.model.User;
import com.chinaunicom.system.service.IUserService;
@Service
public class UserServiceImpl implements IUserService {
	
	@Resource
	private UserMapper dao;

	@Override
	public int deleteByPrimaryKey(Integer nUserid) throws Exception {
		// TODO Auto-generated method stub
		return dao.deleteByPrimaryKey(nUserid);
	}

	@Override
	public int insert(User record) throws Exception {
		// TODO Auto-generated method stub
		return dao.insert(record);
	}

	@Override
	public int insertSelective(User record) throws Exception {
		// TODO Auto-generated method stub
		return dao.insertSelective(record);
	}

	@Override
	public User selectByPrimaryKey(Integer nUserid) throws Exception {
		// TODO Auto-generated method stub
		return dao.selectByPrimaryKey(nUserid);
	}

	@Override
	public int updateByPrimaryKeySelective(User record) throws Exception {
		// TODO Auto-generated method stub
		return dao.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByPrimaryKey(User record) throws Exception {
		// TODO Auto-generated method stub
		return dao.updateByPrimaryKey(record);
	}

	@Override
	public User queryByUserName(String username) throws Exception {
		// TODO Auto-generated method stub
		return dao.querybyUserName(username);
	}

	@Override
	public List<UserDto> query(UserDto dto) throws Exception {
		// TODO Auto-generated method stub
		return dao.query(dto);
	}

	/**
	 * 保存用户数据
	 * 保存用户和角色想关联的数据
	 * 注意： 记得改反向生成的insertSelective方法  主键用序列
	 */
	@Override
	public void save(UserDto dto) throws Exception {
		//1.保存用户数据
		User user = dto.getUser();
		dao.insertSelective(user); //seq_sys_user.nextval
		//2.保存用户和角色的关联关系
		List<Integer> rids = dto.getRids();
		if (rids != null && rids.size()>0) {
			for (Integer rid : rids) {
				//保存一条用户和角色的对应关系  seq_sys_user.currval
				dao.saveUserAndRole(null,rid);
			}
		}
		
	}

	@Override
	public UserDto queryById(Integer id) throws Exception {
		// TODO Auto-generated method stub
		return dao.queryById(id);
	}

	/**
	 * 更新用户信息
	 * 	1.根据id有选择的更新 （因为密码没有修改 密码字段是空的）
	 * 	2.更新和角色的对应关系
	 * 		根据id删除原有关系
	 * 		然后插入新的关系
	 */
	@Override
	public void update(UserDto dto) throws Exception {
		User user = dto.getUser();
		dao.updateByPrimaryKeySelective(user);
		//根据id删除原有关联关系
		dao.deleteRoleByUserId(user.getnUserid());
		List<Integer> list = dto.getRids();
		if (list != null && list.size()>0) {
			for (Integer rid : list) {
				//插入新的关联关系
				dao.saveUserAndRole(user.getnUserid(), rid);
			}
		}
	}

	@Override
	public void delete(Integer id) throws Exception {
		// TODO Auto-generated method stub
		dao.deleteRoleByUserId(id);
		dao.deleteByPrimaryKey(id);
	}

}
